package com.mdlib.helper.module.start.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.common.MDConstant;
import com.mdlib.helper.event.LoginEvent;
import com.mdlib.helper.model.AccountModel;
import com.mdlib.helper.model.AppModel;
import com.mdlib.helper.model.entity.AppFrontInfo;
import com.mdlib.helper.presenter.LoginHelper;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.utils.ToastUtil;
import com.ljlib.core.base.BaseFragment;
import com.ljlib.core.util.LogUtil;
import com.ljlib.help.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by librabin on 16/10/31.
 */

public class LauncherFragment extends BaseFragment {


    public static LauncherFragment newInstance() {
        return new LauncherFragment();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_start;
    }


    @Override
    protected void initView(View rootView) {
        super.initView(rootView);

    }

    @Override
    protected void loadData() {
        super.loadData();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                featchInit();
            }
        }, 800);

    }

    private void featchInit() {
        addSubscribe(DataManager.getInstance().getAccountApi()
                .featchAppFrontInfo()
                .compose(RxHelper.<AppFrontInfo>defaultHandleResult())
                .subscribe(new RxSubscribe<AppFrontInfo>() {
                    @Override
                    protected void onSucc(AppFrontInfo appFrontInfo) {
                        AppModel.getInstance().setAppFrontInfo(appFrontInfo);
                        handLogin();
                    }

                    @Override
                    protected void onFail(String msg) {
                        handLogin();
                    }
                }));


    }


    private void handLogin() {
        int loginType = AccountModel.getInstance().getLoginType();
        String roleType = AccountModel.getInstance().getRoleType();
        LogUtil.e("登陆类型: " + loginType);
        if (loginType == MDConstant.ACCOUNT_TYPE_DEFAULT) {
            String loginName = AccountModel.getInstance().getLoginName();
            String loginPwd = AccountModel.getInstance().getLoginPwd();
            if (!TextUtils.isEmpty(loginName) && !TextUtils.isEmpty(loginPwd)) {
                LoginHelper.doLogin(loginName, loginPwd);
            } else {
                UIHelper.showLogin(getActivity());
            }

        } else if (loginType == MDConstant.ACCOUNT_TYPE_AUTH) {
            if (AccountModel.getInstance().getThirdType() == MDConstant.AUTH_TYPE_WX) {
               com.mdlib.helper.model.entity.AuthUser authUser = new com.mdlib.helper.model.entity.AuthUser(MDConstant.AUTH_TYPE_WX,
                        AccountModel.getInstance().getThirdUid(), "", "", "");
                LoginHelper.doAuthLogin(authUser);
            } else {
                UIHelper.showLogin(getActivity());
            }

        } else {
            UIHelper.showLogin(getActivity());
        }

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(LoginEvent event) {
        stopLoadingDialog();
        if (event.code == MDConstant.STATE_SUCC) {
            ToastUtil.showToast("登录成功");
            AccountModel.getInstance().setLogin(true);
            UIHelper.showMainPage(getActivity());
        } else {
            ToastUtil.showToast(event.msg);
            UIHelper.showLogin(getActivity());
        }
    }


}
